package cn.tedu.store.commons.web;

import cn.tedu.store.commons.ex.ServiceException;
import lombok.Data;

/**
 * 响应的JSON类
 *
 * @author RelentlessFlow
 * @version 0.0.1
 */
@Data
public class JsonResult<T> {
    /**
     * 状态代码
     */
    private Integer status;
    /**
     * 描述信息
     */
    private String message;

    private T data;;

    public static JsonResult<Void> ok() {
        return ok(null);
    }

    public static <T> JsonResult<T> ok(T data){
        JsonResult<T> jsonResult = new JsonResult<>();
        jsonResult.data = data;
        jsonResult.status = 20000;
        return jsonResult;
    }

    public static JsonResult<Void> fail(ServiceException e){
        return fail(e.getServiceCode(),e.getMessage());
    }

    public static JsonResult<Void> fail(ServiceCode status, String message) {
        JsonResult<Void> jsonResult = new JsonResult<>();
        jsonResult.status = status.getValue();
        jsonResult.message = message;
        return jsonResult;
    }
}
